package base.conn;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.net.telnet.TelnetClient;
import org.apache.log4j.Logger;

/**
 * 通过telnet方式取告警
 * 
 * @author Administrator
 * 
 */

public class TelnetConn {

	Logger log = Logger.getLogger(TelnetConn.class);

	private TelnetClient telnet;
	private BufferedReader br;

	private InputStream in;
	private PrintStream out;

	/**
	 * 告警字符集
	 */
	private String charCode = "gbk";

	/**
	 * ip
	 */
	private String ip;

	/**
	 * 端口
	 */
	private int port;

	/**
	 * 存放取到的告警信息
	 */
	private List<String> dealMessagePool = Collections.synchronizedList(new LinkedList<String>());// 数据列表容器

	/**
	 * 登录指令及跳转
	 */
	private List<String> loginStr = null;

	/**
	 * 取telnet连接,如果port<=0,会设置为23
	 * 
	 * @param ip
	 * @param port
	 */
	public void getConn(String _ip, int _port) {
		try {

			if (_port <= 0) {
				_port = 23;
			}
			log.info("ip=" + _ip);
			log.info("port=" + _port);

			ip = _ip;
			port = _port;

			telnet = new TelnetClient();
			telnet.setConnectTimeout(60 * 1000);
			telnet.connect(ip, port);
			telnet.setSoTimeout(100 * 1000);

			Thread.sleep(2000);
			in = telnet.getInputStream();
			out = new PrintStream(telnet.getOutputStream());

			br = new BufferedReader(new InputStreamReader(telnet.getInputStream(), charCode));

			if (loginStr != null) {
				this.login();
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error(ip + ">>" + port + ">>连接错误>>" + e);

			log.info("=10s retry...");
			try {
				Thread.sleep(10 * 1000);
			} catch (Exception e1) {
			}
			getConn(ip, port);

		}
	}

	public String sendCommand(String command) {
		if (out == null) {
			this.getConn(this.ip, this.port);
		}
		out.println(command);
		out.flush();
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
		}

		return this.getResultByReadLine();
	}

	private String getResult() {
		if (in == null) {
			this.getConn(this.ip, this.port);
		}
		String str = "";
		try {
			while (true) {
				int count = in.available();
				if (count == 0) {
					break;
				}
				byte[] bytes = new byte[count];
				in.read(bytes);
				str += new String(bytes, charCode);
				Thread.sleep(200);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error("取结果异常=" + e);
		}
		return str;
	}

	private void login() {
		if (out == null) {
			this.getConn(this.ip, this.port);
		}
		for (String s : loginStr) {

			out.println(s);
			out.flush();
			try {
				Thread.sleep(500);
			} catch (Exception e) {
			}
			String rs = this.getResultByReadLine();
			log.info(s + ">> return:" + rs);
		}
	}

	/**
	 * 通过readLine信息并返回
	 * 
	 * @param list
	 */
	public String getResultByReadLine() {

		if (br == null) {
			getConn(ip, port);
		}
		String s = "";
		while (true) {
			try {
				if (br.ready()) {
					s += br.readLine() + "\n";
				} else {
					break;
				}
				if (s == null || "".equals(s)) {
					Thread.sleep(3 * 1000);
					continue;
				}
			} catch (Exception e) {
				e.printStackTrace();
				log.error("取告警异常=" + e);
				log.info("重新连接");
				getConn(ip, port);
			}
		}
		return s;

	}

	public TelnetClient getTelnet() {
		return telnet;
	}

	public void setTelnet(TelnetClient telnet) {
		this.telnet = telnet;
	}

	public BufferedReader getBr() {
		return br;
	}

	public void setBr(BufferedReader br) {
		this.br = br;
	}

	public InputStream getIn() {
		return in;
	}

	public void setIn(InputStream in) {
		this.in = in;
	}

	public PrintStream getOut() {
		return out;
	}

	public void setOut(PrintStream out) {
		this.out = out;
	}

	public String getCharCode() {
		return charCode;
	}

	public void setCharCode(String charCode) {
		this.charCode = charCode;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public List<String> getDealMessagePool() {
		return dealMessagePool;
	}

	public void setDealMessagePool(List<String> dealMessagePool) {
		this.dealMessagePool = dealMessagePool;
	}

	public List<String> getLoginStr() {
		return loginStr;
	}

	public void setLoginStr(List<String> loginStr) {
		this.loginStr = loginStr;
	}

}
